package com.zys.controller;

import com.zys.entity.Result;
import com.zys.pojo.Merchant;
import com.zys.entity.QueryInfo;
import com.zys.entity.PageResult;
import com.zys.service.MerchantService;
import com.zys.constant.MessageConstant;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 处理商家 Controller 层
 */
@RestController
@RequestMapping("/merchant")
public class MerchantController {

  @Reference
  private MerchantService merchantService;

  /**
   * 根据条件获取商家列表
   * @param queryInfo 条件
   * @return PageResult
   */
  @RequestMapping("/getMerchantList")
  public PageResult<Merchant> getMerchantList(@RequestBody QueryInfo queryInfo) {
    try {
      return merchantService.getMerchantList(queryInfo);
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }

  /**
   * 添加商家
   *
   * @param merchant 商家信息
   * @return Result
   */
  @RequestMapping("/add")
  public Result add(@RequestBody Merchant merchant) {
    try {
      merchantService.add(merchant);
      return new Result(true, MessageConstant.ADD_MERCHANT_SUCCESS);
    } catch (Exception e) {
      e.printStackTrace();
      return new Result(false, MessageConstant.ADD_MERCHANT_FAIL);
    }
  }

  /**
   * 删除商家
   *
   * @param id 商家 id
   * @return Result
   */
  @RequestMapping("/delete")
  public Result delete(@RequestParam Integer id) {
    try {
      merchantService.deleteById(id);
      return new Result(true, MessageConstant.DELETE_MERCHANT_SUCCESS);
    } catch (Exception e) {
      e.printStackTrace();
      return new Result(false, MessageConstant.DELETE_MERCHANT_FAIL);
    }
  }

  /**
   * 根据 id 获取商家信息
   *
   * @param id 商家 id
   * @return Result
   */
  @RequestMapping("/getMerchantById")
  public Result getUserById(@RequestParam Integer id) {
    try {
      return new Result(true, MessageConstant.GET_MERCHANT_SUCCESS, merchantService.findMerchantById(id));
    } catch (Exception e) {
      e.printStackTrace();
      return new Result(false, MessageConstant.GET_MERCHANT_FAIL);
    }
  }

  /**
   * 修改商家信息
   *
   * @param merchant 修改后的商家信息
   * @return Result
   */
  @RequestMapping("/edit")
  public Result edit(@RequestBody Merchant merchant) {
    try {
      merchantService.edit(merchant);
      return new Result(true, MessageConstant.EDIT_MERCHANT_SUCCESS);
    } catch (Exception e) {
      e.printStackTrace();
      return new Result(false, MessageConstant.EDIT_MERCHANT_FAIL);
    }
  }

  /**
   * 修改商家角色
   *
   * @param mcId   商家 id
   * @param roleId 角色 id
   * @return Result
   */
  @RequestMapping("/changeMerchantRole")
  public Result changeMerchantRole(Integer mcId, Integer roleId) {
    try {
      merchantService.changeMerchantRole(mcId, roleId);
      return new Result(true, MessageConstant.CHANGE_MERCHANT_ROLE_SUCCESS);
    } catch (Exception e) {
      return new Result(false, MessageConstant.CHANGE_MERCHANT_ROLE_FAIL);
    }
  }
}
